package com.itheima.pinda.impl.transportline;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.entity.dto.PdTransportLineDTO;
import com.itheima.pinda.DTO.entity.pojo.transportline.PdTransportLine;
import com.itheima.pinda.DTO.entity.pojo.transportline.PdTransportLineType;
import com.itheima.pinda.feign.service.transportline.PdTransportLineService;
import com.itheima.pinda.feign.service.transportline.PdTransportLineTypeService;
import com.itheima.pinda.mapper.transportline.PdTransportLineMapper;
import com.itheima.pinda.utils.CustomIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 线路表 服务实现类
 * </p>
 *
 * @author xy
 * @since 2021-11-15
 */
@Service
public class PdTransportLineServiceImpl extends ServiceImpl<PdTransportLineMapper, PdTransportLine> implements PdTransportLineService {

    @Autowired
    private CustomIdGenerator idGenerator;

    @Autowired
    private PdTransportLineTypeService transportLineTypeService;

    /**
     * 线路管理中的查询所有， 下怒管理中有线路类型名称，所以需要连表查询，连接PdTransportLineType查询出类型名称
     * @param lineNumber
     * @param name
     * @param lineTypeId
     * @return
     */
    @Override
    public List<PdTransportLineDTO> findAll(String lineNumber, String name, String lineTypeId) {

//        根据条件查询出
        QueryWrapper<PdTransportLine> wrapper = new QueryWrapper<>();
        wrapper.like("name",name).eq("type_number",lineTypeId).eq("id",lineNumber);
        List<PdTransportLine> transportLineList = super.baseMapper.selectList(wrapper);


        List<PdTransportLineDTO> transportLineDTOArrayList = new ArrayList<>();


//        查询类型名称，并添加进集合
        for (PdTransportLine transport: transportLineList
             ) {
            PdTransportLineDTO transportLineDTO = new PdTransportLineDTO();
            BeanUtils.copyProperties(transport,transportLineDTO);

            PdTransportLineType transportLineType = transportLineTypeService.getBaseMapper().selectById(lineTypeId);

            transportLineDTO.setLineTypeName(transportLineType.getName());

            transportLineDTOArrayList.add(transportLineDTO);


        }

        return transportLineDTOArrayList;
    }

    @Override
    public Integer disableTransportLine(String lineNumber) {
        if (ObjectUtils.isEmpty(lineNumber)){
            throw new NullPointerException("对象为空");
        }
        PdTransportLine transportLine = super.baseMapper.selectById(lineNumber);
        transportLine.setStatus(0);
        int i = super.baseMapper.updateById(transportLine);
        return i;
    }

    @Override
    public Integer updateTransportLine(PdTransportLine transportLine) {
        if (ObjectUtils.isEmpty(transportLine)){
            throw new NullPointerException("对象为空");
        }
        int i = super.baseMapper.updateById(transportLine);
        return i;
    }

    @Override
    public Integer insertTranssportLine(PdTransportLine transportLine) {
        if (ObjectUtils.isEmpty(transportLine)){
            throw new NullPointerException("对象为空");
        }
        int i = super.baseMapper.insert(transportLine);
        return i;
    }
}
